package com.soft.idc.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.json.JacksonFactory;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.soft.idc.common.control.AccessManagerController;
import com.soft.idc.common.control.AgentManagerController;
import com.soft.idc.common.control.AirManagerController;
import com.soft.idc.common.control.AuthInterceptor;
import com.soft.idc.common.control.AuthTemplateManagerController;
import com.soft.idc.common.control.ComponentManagerController;
import com.soft.idc.common.control.CacheController;
import com.soft.idc.common.control.DeviceManagerController;
import com.soft.idc.common.control.DeviceTypeManagerController;
import com.soft.idc.common.control.IndexController;
import com.soft.idc.common.control.KpiDefineManagerController;
import com.soft.idc.common.control.KpiTaskManagerController;
import com.soft.idc.common.control.LoginController;
import com.soft.idc.common.control.MenuConfigAuthController;
import com.soft.idc.common.control.MenuManagerController;
import com.soft.idc.common.control.OrgManagerController;
import com.soft.idc.common.control.RoleManagerController;
import com.soft.idc.common.control.TableMappingController;
import com.soft.idc.common.control.UserMangerController;
import com.soft.idc.common.control.WarnRuleManagerController;
import com.soft.idc.common.control.WarnTypeManagerController;
import com.soft.idc.common.model._MappingKit;
import com.soft.idc.module.device.DeviceController;
import com.soft.idc.module.flight.control.FlightController;
import com.soft.idc.module.flow.FlowController;
import com.soft.idc.module.mainpage.MainPageController;
import com.soft.idc.module.nettopo.NetTopoController;

public class WebConfig extends JFinalConfig {

	private Boolean isCacheServer = false;

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {

		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("a_little_config.txt");
		me.setDevMode(PropKit.getBoolean("devMode", false));

		isCacheServer = PropKit.getBoolean("cacheServer");
		if (isCacheServer) {
			me.setJsonFactory(new JacksonFactory());
		}
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		if (!isCacheServer) {
			me.add("/", IndexController.class);
			me.add("/login", LoginController.class);
			me.add("/user", UserMangerController.class);
			me.add("/role", RoleManagerController.class);
			me.add("/auth_template", AuthTemplateManagerController.class);
			me.add("/menu", MenuManagerController.class);
			me.add("/menu_config_auth", MenuConfigAuthController.class);
			me.add("/org", OrgManagerController.class);
			me.add("/air", AirManagerController.class);
			me.add("/device_type", DeviceTypeManagerController.class);
			me.add("/deviceManager", DeviceManagerController.class);
			me.add("/kpiDefine", KpiDefineManagerController.class);
			me.add("/kpiTask", KpiTaskManagerController.class);
			me.add("/agent", AgentManagerController.class);
			me.add("/access", AccessManagerController.class);
			me.add("/component", ComponentManagerController.class);
			me.add("/tableMapping", TableMappingController.class);
			me.add("/warn_type", WarnTypeManagerController.class);
			me.add("/warn_rule", WarnRuleManagerController.class);
			// me.add("/login", IndexController.class, "/login");

			// 业务模块
			me.add("/flight", FlightController.class);
			me.add("/mainpage", MainPageController.class);
			me.add("/device", DeviceController.class);
			me.add("/nettopo", NetTopoController.class);
			me.add("/flow", FlowController.class);
			// 后台用户管理
			// me.add("/user", BlogController.class);
		} else {
			me.add("/cache", CacheController.class);
		}

	}

	public static C3p0Plugin createC3p0Plugin() {
		return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		if (!isCacheServer) {
			// 配置C3p0数据库连接池插件
			C3p0Plugin C3p0Plugin = createC3p0Plugin();
			me.add(C3p0Plugin);

			// 配置ActiveRecord插件
			ActiveRecordPlugin arp = new ActiveRecordPlugin(C3p0Plugin);
			me.add(arp);

			// ehcached
			me.add(new EhCachePlugin());

			// 所有配置在 MappingKit 中搞定
			_MappingKit.mapping(arp);

		} else {
			System.setProperty("net.sf.ehcache.enableShutdownHook", "true");
			me.add(new EhCachePlugin());
		}

	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		// me.addGlobalActionInterceptor(new AuthInterceptor());// 授权拦截
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {

	}

	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目 运行此 main
	 * 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 8080, "/", 5);
	}
}
